package models

import (
	"time"

	"gorm.io/gorm"
)

// Bill 账单
type Bill struct {
	BillID int `gorm:"primary_key" json:"bill_id"` // ID

	StreamerId    int       `json:"streamer_id"`    // 主播ID
	OutOrderSn    string    `json:"out_order_sn"`   // 线上订单号
	ExpressNo     string    `json:"express_no"`     // 快递单号
	ExpressName   string    `json:"express_name"`   // 快递名称
	Weight        float64   `json:"weight"`         // 订单重量
	Freight       float64   `json:"freight"`        // 运费
	InstitutionId int       `json:"institution_id"` // 机构ID
	GoodsID       int       `json:"goods_id"`       // 商品ID
	SellingPrice  int       `json:"selling_price"`  // 卖货价
	SupplyPrice   int       `json:"supply_price"`   // 供应价
	CreateTime    time.Time `json:"create_time"`    // 创建时间
	UpdateTime    time.Time `json:"update_time"`    // 更新时间

	Streamer Streamer
	Goods    Goods
}

// TableName 表名称
func (*Bill) TableName() string {
	return "bill"
}

// GetBill 查询多条数据
func GetBill(fields []string, maps map[string]interface{}) ([]*Bill, error) {
	var address []*Bill
	cond, vals, errs := WhereBuild(maps)
	if errs != nil {
		return nil, errs
	}
	err := db.Select(fields).Where(cond, vals...).Take(&address).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return address, nil
}

func GetBillPage(fields []string, maps map[string]interface{}, pageNum int, pageSize int, order string) ([]*Bill, error) {
	var Bill []*Bill
	cond, vals, errs := WhereBuild(maps)
	if errs != nil {
		return nil, errs
	}
	err := db.Select(fields).Preload("Streamer").Preload("Goods").Where(cond, vals...).Offset(pageNum).Limit(pageSize).Order(order).Find(&Bill).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return Bill, nil
}

// GetBillInfo 查询多条数据
func GetBillInfo(fields []string, maps map[string]interface{}) (*Bill, error) {
	var Bill Bill
	cond, vals, errs := WhereBuild(maps)
	if errs != nil {
		return nil, errs
	}
	err := db.Select(fields).Where(cond, vals...).Take(&Bill).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return &Bill, nil
}

// AddBill 插入数据
func AddBill(data map[string]interface{}) (int, error) {
	address := Bill{
		StreamerId:    data["streamer_id"].(int),     // 主播ID
		OutOrderSn:    data["out_order_sn"].(string), // 线上订单号
		ExpressNo:     data["express_no"].(string),   // 快递单号
		ExpressName:   data["express_name"].(string), // 快递名称
		Weight:        data["weight"].(float64),      // 订单重量
		Freight:       data["freight"].(float64),     // 运费
		InstitutionId: data["institution_id"].(int),  // 机构ID
		GoodsID:       data["goods_id"].(int),        // 商品ID
		SellingPrice:  data["selling_price"].(int),   // 卖货价
		SupplyPrice:   data["supply_price"].(int),    // 供应价
	}
	result := db.Select("streamer_id", "out_order_sn", "express_no", "express_name", "weight", "freight", "institution_id", "goods_id", "selling_price", "supply_price").Create(&address)
	if err := result.Error; err != nil {
		return 0, err
	}
	return address.BillID, nil
}

// EditBill modify a single Bill
func EditBill(where map[string]interface{}, data interface{}) error {
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return errs
	}
	if err := db.Model(&Bill{}).Where(cond, vals...).Updates(data).Error; err != nil {
		return err
	}
	return nil
}

func GetBillTotal(where map[string]interface{}) (int64, error) {
	var count int64
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return 0, errs
	}
	if err := db.Model(&Bill{}).Where(cond, vals...).Count(&count).Error; err != nil {
		return 0, err
	}
	return count, nil
}

func CleanBill(where map[string]interface{}) error {
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return errs
	}
	if err := db.Unscoped().Where(cond, vals...).Delete(&Bill{}).Error; err != nil {
		return err
	}

	return nil
}
